package com.mantou.scucorse.core.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.mantou.scucorse.core.entity.User;
import com.mantou.scucorse.core.scheduler.CourseNetInvoker;
import com.mantou.scucorse.core.service.UserService;
import com.mantou.scucorse.utils.ReturnCode;
import com.mantou.scucorse.utils.ReturnContainer;

@Controller
@SessionAttributes("userId")
@RequestMapping("/scucorse")
public class UserRestApiController {

	private final Logger logger = LoggerFactory.getLogger(UserRestApiController.class);
	
	
	@Resource
	private UserService userService;
	
	@Resource
	private CourseNetInvoker courseNetInvoker;
	
	@RequestMapping(value = "/core/user", method = RequestMethod.GET)
	public ResponseEntity<ReturnContainer<User>> getUserById(HttpSession session,Model model){
		Map<String,Object> modelMap = model.asMap();
		if(!modelMap.containsKey("userId")){
			return new ResponseEntity<ReturnContainer<User>>(new ReturnContainer<User>(ReturnCode.NOTLOGIN.getStatus(),ReturnCode.NOTLOGIN.getDescription(),null), HttpStatus.OK);
		}
		Integer userId = (Integer) modelMap.get("userId");
		User user = userService.findByPrimaryKey(userId);
		user.setPassword(null);
		return new ResponseEntity<ReturnContainer<User>>(new ReturnContainer<User>(ReturnCode.OK.getStatus(),ReturnCode.OK.getDescription(),user), HttpStatus.OK);
	}
	
	@RequestMapping(value = "/core/user", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<ReturnContainer<Integer>> createUser(HttpSession session,@RequestBody User userVO,Model model){
		User existUser = userService.getByStudentNumber(userVO.getStudentnumber());
		if(existUser != null){
			if(existUser.getPassword() != null && existUser.getPassword().equals(userVO.getPassword())){
				model.addAttribute("userId", existUser.getId());
				return new ResponseEntity<ReturnContainer<Integer>>(new ReturnContainer<Integer>(ReturnCode.OK.getStatus(),ReturnCode.OK.getDescription(),1), HttpStatus.OK);
			}else{
				return new ResponseEntity<ReturnContainer<Integer>>(new ReturnContainer<Integer>(ReturnCode.PASSWORDERROR.getStatus(),ReturnCode.PASSWORDERROR.getDescription(),0), HttpStatus.OK);
			}
		}
		userVO.setFailedtimes(null);
		userVO.setProxyid(null);
		userVO.setStatus(null);
		Integer  ueserid = userService.createSelective(userVO);
		model.addAttribute("userId", ueserid);
		
		//boolean islogin = courseNetInvoker.login(ueserid);
		//Integer result = islogin?1:0;
		return new ResponseEntity<ReturnContainer<Integer>>(new ReturnContainer<Integer>(ReturnCode.OK.getStatus(),ReturnCode.OK.getDescription(),1), HttpStatus.OK);
	}
	
	@RequestMapping(value = "/core/user/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<Integer> deleteUserByPrimaryKey(@PathVariable Integer id){
		Integer  result = userService.deleteByPrimaryKey(id);
		Integer responseEnv = new Integer(result);
		return new ResponseEntity<Integer>(responseEnv, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/core/user/", method = RequestMethod.PUT)
	public ResponseEntity<ReturnContainer<Integer>> updateUserByPrimaryKeySelective(HttpSession session,Model model, @RequestBody User userVO){
		Map<String,Object> modelMap = model.asMap();
		if(!modelMap.containsKey("userId")){
			return new ResponseEntity<ReturnContainer<Integer>>(new ReturnContainer<Integer>(ReturnCode.OK.getStatus(),ReturnCode.OK.getDescription(),1), HttpStatus.OK);
		}
		Integer userId = (Integer) modelMap.get("userId");
		userVO.setId(userId);
		userVO.setFailedtimes(null);
		userVO.setProxyid(null);
		//userVO.setStatus(null);
		userVO.setStudentnumber(null);
		if(userVO.getStatus() != null && (userVO.getStatus() ==3 || userVO.getStatus() == 6)){
		}else{
			userVO.setStatus(null);
		}
		userService.updateByPrimaryKeySelective(userVO);
		return new ResponseEntity<ReturnContainer<Integer>>(new ReturnContainer<Integer>(ReturnCode.OK.getStatus(),ReturnCode.OK.getDescription(),1), HttpStatus.OK);
	}
	
}
